import { defineStore } from 'pinia';
import { ref } from 'vue';
import { CommonApi } from '@/api/common';
import { useRequest } from '@/hooks/useRequest';
import { isApp } from '@/utils/platform.utils';
import { useDictStore } from './dictStore';

export const useAppStore = defineStore('app', () => {
  const dictStore = useDictStore();

  // 页面缩放比例
  const scale = ref(1);

  // 页面缩放比例
  const setScale = (val: number) => {
    scale.value = val;
  };

  // 医院信息
  const {
    data: hospitalInfo,
    runAsync: getHospitalInfo,
    timeTask: hospitalInfoTimeTask
  } = useRequest(() => CommonApi.getHospitalInfo(), {
    manual: true,
    pollingInterval: 1000 * 60 * 5,
    manualPolling: true
  });

  let wxcPlugin;
  // #ifdef APP-PLUS
  // eslint-disable-next-line prefer-const
  wxcPlugin = uni.requireNativePlugin('XWC-Util');
  // #endif
  const enterFullscreen = () => {
    if (isApp) {
      wxcPlugin.fullScreen();
    }
  };

  return {
    hospitalInfo,
    scale,
    setScale,
    setupApp() {
      enterFullscreen();
      dictStore.handleGetDictInfo();
      getHospitalInfo().finally(() => {
        hospitalInfoTimeTask();
      });
    }
  };
});
